/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-11-30     Administrator       the first version
 */
#include <key_check.h>
#include <key_board.h>
#include <key_board_sample.h>
#include <task.h>
#include <mygpio.h>
#include <board.h>
#include <rtthread.h>
//按键标志结构体
KeyFlag_t KeyFlag;
//按键组合返回值
unsigned int test_id1, test_id2;
//注册组合状态
const struct key_combine_t test_combine1[] = {
        { .id = KEY_SET,     .state = KEY_PRESS },
        { .id = KEY_DOWN,   .state = KEY_PRESS },
};
const struct key_combine_t test_combine2[] = {
        { .id = KEY_UP,     .state = KEY_PRESS },
        { .id = KEY_DOWN,   .state = KEY_PRESS },
        { .id = KEY_UP,     .state = KEY_PRESS },
        { .id = KEY_DOWN,   .state = KEY_PRESS },
};
//返回接收值
unsigned int res;

//注册组合事件
void register_event(void)
{
    test_id1 = key_combine_register(test_combine1, GET_ARRAY_SIZE(test_combine1));
    test_id2 = key_combine_register(test_combine2, GET_ARRAY_SIZE(test_combine2));
}

rt_uint8_t PhotoElectricityStop = 0;
//按键事件检测
void KEY_CHECK(void)
 {
    //up
    //-----------------------------UP-----------------------------
    if(key_check_state(KEY_UP, KEY_RELEASE))
    {
        rt_kprintf("KEY_UP KEY_RELEASE\r\n");
    }
    if(key_check_state(KEY_UP, KEY_PRESS))
    {

        //---------------------调整转速up----------------------
        //调整电机的转速，为2是最高档转速，不作操作

        //进入了设置模式
        if (KeyFlag.Key_set_f == 1) {
            Sensitivity+=0.1; //每按一次加一定值
        }else {
            //非设置模式，切换转速
            if (KeyFlag.key_up_data == 2) {

            }else if(KeyFlag.Key_switch_f == 1)
            {
                KeyFlag.key_up_data++;
            }
        }
        rt_kprintf("KEY_UP KEY_PRESS %d\r\n",KeyFlag.key_up_data);

    }
    if(key_check_state(KEY_UP, KEY_PRESS_LONG))
    {
        rt_kprintf("KEY_UP KEY_PRESS_LONG\r\n");
    }
    if(key_check_state(KEY_UP, KEY_RELEASE_LONG))
    {
        rt_kprintf("KEY_UP KEY_RELEASE_LONG\r\n");
    }

    res = key_check_state(KEY_UP, KEY_PRESS_MULTI);
    if(res)
    {
        rt_kprintf("KEY_UP KEY_PRESS_MULTI:%d\r\n", res);
    }
    res = key_check_state(KEY_UP, KEY_RELEASE_MULTI);
    if(res)
    {
        rt_kprintf("KEY_UP KEY_RELEASE_MULTI:%d\r\n", res);
    }
    if(key_check_state(KEY_UP, KEY_PRESS_CONTINUOUS))
    {
        rt_kprintf("KEY_UP KEY_PRESS_CONTINUOUS\r\n");
    }
    //------------------------DOWN----------------------------------
    //down
    if(key_check_state(KEY_DOWN, KEY_RELEASE))
    {
        rt_kprintf("KEY_DOWN KEY_RELEASE\r\n");
    }
    if(key_check_state(KEY_DOWN, KEY_PRESS))
    {
        //---------------------调整转速down----------------------
        //进入设置模式
        if (KeyFlag.Key_set_f == 1)
        {
            if (Sensitivity <= 0.1) {
                //为下限不作操作
            }else {
                Sensitivity-=0.1; //每按一次加一定值
            }
        }else
        {
            //非设置模式，切换转速
            if (KeyFlag.key_up_data == 0) {

            }else if(KeyFlag.Key_switch_f == 1)
            {
                KeyFlag.key_up_data--;
            }
        }

        rt_kprintf("KEY_DOWN KEY_PRESS %d\r\n",KeyFlag.key_up_data);
    }
    if(key_check_state(KEY_DOWN, KEY_PRESS_LONG))
    {
        rt_kprintf("KEY_DOWN KEY_PRESS_LONG\r\n");
    }
    if(key_check_state(KEY_DOWN, KEY_RELEASE_LONG))
    {
        rt_kprintf("KEY_DOWN KEY_RELEASE_LONG\r\n");
    }
    res = key_check_state(KEY_DOWN, KEY_PRESS_MULTI);
    if(res)
    {
        rt_kprintf("KEY_DOWN KEY_PRESS_MULTI:%d\r\n", res);
    }
    res = key_check_state(KEY_DOWN, KEY_RELEASE_MULTI);
    if(res)
    {
        rt_kprintf("KEY_DOWN KEY_RELEASE_MULTI:%d\r\n", res);
    }
    if(key_check_state(KEY_DOWN, KEY_PRESS_CONTINUOUS))
    {
        rt_kprintf("KEY_DOWN KEY_PRESS_CONTINUOUS\r\n");
    }

    //SET
    //-----------------------------SET----------------------------
    if(key_check_state(KEY_SET, KEY_RELEASE))
    {
        rt_kprintf("KEY_SET KEY_RELEASE\r\n");
    }
    if(key_check_state(KEY_SET, KEY_PRESS))
    {
        rt_kprintf("KEY_SET KEY_PRESS %d\r\n",KeyFlag.Key_set_f);
    }
    if(key_check_state(KEY_SET, KEY_PRESS_LONG))
    {
        //--------------------设置按键长按----------------------
        //只有在悬浮模式下才能进入设置
        //第一次长按进入设置，第二次长按表示确认
        if (KeyFlag.Key_set_f == 1 && KeyFlag.Key_switch_f == 1)
        {
            KeyFlag.Key_set_f = 0;//手握模式
            //----确认code-----

        }else if(KeyFlag.Key_switch_f == 1)
        {
            KeyFlag.Key_set_f = 1;//悬浮模式
        }
        rt_kprintf("KEY_SET KEY_PRESS_LONG %d\r\n",KeyFlag.Key_set_f);
    }
    if(key_check_state(KEY_SET, KEY_RELEASE_LONG))
    {
        rt_kprintf("KEY_SET KEY_RELEASE_LONG\r\n");
    }
    res = key_check_state(KEY_SET, KEY_PRESS_MULTI);
    if(res)
    {
        if (res == 2) {
            //关闭、开启光电检测
            if (PhotoElectricityStop == 0) {
                PhotoElectricityStop = 1;
            }else {
                PhotoElectricityStop = 0;
            }
        }
        rt_kprintf("KEY_SET KEY_PRESS_MULTI:%d %d\r\n", res,PhotoElectricityStop);
    }
    res = key_check_state(KEY_SET, KEY_RELEASE_MULTI);
    if(res)
    {
        rt_kprintf("KEY_SET KEY_RELEASE_MULTI:%d\r\n", res);
    }
    if(key_check_state(KEY_SET, KEY_PRESS_CONTINUOUS))
    {
        rt_kprintf("KEY_SET KEY_PRESS_CONTINUOUS\r\n");
    }

    //SWITCH
    //-----------------------------SWITCH----------------------------
    if(key_check_state(KEY_SWITCH, KEY_RELEASE))
    {
        rt_kprintf("KEY_SWITCH KEY_RELEASE\r\n");
    }
    if(key_check_state(KEY_SWITCH, KEY_PRESS))
    {
        if (KeyFlag.Key_switch_f == 1) {
            //手握模式
            KeyFlag.Key_switch_f = 0;
        }else {
            //悬浮模式
            KeyFlag.Key_switch_f = 1;
            //停止电机
            Motor_Stop();
            //电机检测状态复位
            DetectionStatusReset();
        }
        rt_kprintf("KEY_SWITCH KEY_PRESS Key_switch_f %d\r\n",KeyFlag.Key_switch_f);
    }
    if(key_check_state(KEY_SWITCH, KEY_PRESS_LONG))
    {
        rt_kprintf("KEY_SWITCH KEY_PRESS_LONG\r\n");
    }
    if(key_check_state(KEY_SWITCH, KEY_RELEASE_LONG))
    {
        rt_kprintf("KEY_SWITCH KEY_RELEASE_LONG\r\n");
    }
    res = key_check_state(KEY_SWITCH, KEY_PRESS_MULTI);
    if(res)
    {
        rt_kprintf("KEY_SWITCH KEY_PRESS_MULTI:%d\r\n", res);
    }
    res = key_check_state(KEY_SWITCH, KEY_RELEASE_MULTI);
    if(res)
    {
        rt_kprintf("KEY_SWITCH KEY_RELEASE_MULTI:%d\r\n", res);
    }
    if(key_check_state(KEY_SWITCH, KEY_PRESS_CONTINUOUS))
    {
        rt_kprintf("KEY_SWITCH KEY_PRESS_CONTINUOUS\r\n");
    }

    //组合按键
    //-------------------------组合按键---------------------------------
    if(key_check_combine_state(test_id1))
    {

        rt_kprintf("combine test_id1 %d\r\n",PhotoElectricityStop);
    }
//
//    if(key_check_combine_state(test_id2))
//    {
//        rt_kprintf("combine test_id2\r\n");
//    }

    //急停按键 按下为0 弹起为1
    //-------------------------急停按键---------------------------------
    //如果按下了
    if (!rt_pin_read(GET_PIN(C,14)))
    {
        //首先停止电机
        Motor_Stop();
        //电机检测状态复位
        DetectionStatusReset();
        //恢复线程2
        rt_thread_resume(&handle_thead_02_print);
        //挂起线程Thread_03_DAQ
        rt_thread_suspend(&handle_thead_03_DAQ);
        rt_schedule();
    }

 }

//急停按键弹起检测
void EmergencyStop_Check(void)
{
    if (rt_pin_read(GET_PIN(C,14))) {
        //恢复线程3
        rt_thread_resume(&handle_thead_03_DAQ);
        rt_pin_write(GET_PIN(A,1) , PIN_HIGH);
        //挂起线程THREAD_02_PRINT_PRIOR
        rt_thread_suspend(&handle_thead_02_print);
        rt_schedule();
    }
}

rt_uint8_t LevitationStop = 0;
//光电传感器状态检测
void PhotoElectricity_Check (void)
{
    //按键是否按下判断
    if (!PhotoElectricityStop)
    {
//        rt_kprintf("stop %d\n",PhotoElectricityStop);
        //1是悬浮 0是手握
        if (!rt_pin_read(GET_PIN(C,15))) {
            //手握
            KeyFlag.Key_switch_f = 0;
            //电机停止标志
            if (!LevitationStop) {
                LevitationStop = 1;
            }
        }else {
            //悬浮
            KeyFlag.Key_switch_f = 1;
            //进入悬浮的时候只在最初的时候停止一次
            if (LevitationStop) {
                Motor_Stop();
                //电机检测状态复位
                DetectionStatusReset();
                LevitationStop = 0;
            }
        }
    }
}




























